#include "ItemReference.h"
//#include "ItemReferenceFinder.h"

#include "Defines.h"
#include "CampaignItem.h"



#include <QCoreApplication>
#include <QTimer>


namespace
{
QString REF_FIELD_NAME = "ref";
const char* INVALID_REFERENCE_TEXT = QT_TRANSLATE_NOOP("ItemReference","Invalid Reference");

}


ItemReference::ItemReference(AbstractItem* parent)
    : Item("Reference", parent)
{
    d = new Data();
    setData(CategoryData, ItemCategory_Reference);
}

ItemReference::ItemReference(const QString& refName, AbstractItem *parent)
    : Item("Reference", parent)
{
    d = new Data();
    setData(CategoryData, ItemCategory_Reference);
    setData(ValueData, refName);
}



ItemReference::~ItemReference()
{
}



bool ItemReference::isValid() const
{
    if (d->refItem.isNull()) return false;
    if (d->refName.isEmpty()) return false;
    if (d->refName != d->refItem->objectName()) return false;

    return true;
}



int ItemReference::fieldCount() const
{
    return (ItemReference::DataType_LAST - AbstractItem::DataType_FIRST + 1);
}



QString ItemReference::fieldName(const int &data_type) const
{
    switch (data_type)
    {
    case ValueData:  return REF_FIELD_NAME;
    }

    return QString::null;
}



int ItemReference::field(const QString &field_name) const
{
    if (field_name.toLower() == REF_FIELD_NAME)    return ValueData;

    return AbstractItem::field(field_name);
}



QString ItemReference::referenceName() const
{
    return data(ValueData).toString();
}



Item* ItemReference::referenceItem() const
{
    return d->refItem.data();
}



void ItemReference::refreshReference()
{
    Item* refItem = rootItem()->findChild<Item*>(d->refName);
    if (!refItem)
    {
        d->refName = QString::null;
        d->refItem = 0;
        emit sigDirty();
        return;
    }

    if (!parent() || refItem == parent()) return;

    foreach (QObject* obj, parent()->children())
    {
        ItemReference* otherRefItem = dynamic_cast<ItemReference*>(obj);
        if (!otherRefItem) continue;

        if (otherRefItem->referenceItem() == refItem) return;
    }

    d->refItem = refItem;
    d->refName = refItem->objectName();

    emit sigDirty();
}


QVariant ItemReference::customData(const int &data_type) const
{
    switch (data_type)
    {
    case Item::ValueData:
        {
            if (!isValid()) return qApp->translate("ItemReference", INVALID_REFERENCE_TEXT);

            return d->refName;
        }
    case AbstractItem::DescData:
        {
            return AbstractItem::objectName();
        }

    case AbstractItem::NameData:
        {
            return AbstractItem::data(data_type).toString();
        }
    }

    return QVariant();
}



void ItemReference::setCustomData(const int &data_type, const QVariant &value)
{
    switch (data_type)
    {
    case ValueData:
        if (value.toString() == INVALID_REFERENCE_TEXT) return;
        if (d->refName == value.toString()) return;

        d->refName = value.toString();
        break;

    default:
        return;
    }


    QTimer::singleShot(0, this, SLOT(refreshReference()));
}



